﻿using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using VupSlashLauncher.Common;
using VupSlashLauncher.Model;

namespace VupSlashLauncher.View
{
    public partial class IManulUpdateBox : Window
    {
        private bool isUpdated = false;
        private bool addedDirectory = false;

        public IManulUpdateBox()
        {
            InitializeComponent();
        }
        private void WIndow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Resources.Add("TColor", new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalValues.ThemeColor)));
            Color c = (Color)ColorConverter.ConvertFromString((GlobalValues.ThemeColor));
            if ((c.R * 30 + c.G * 59 + c.B * 11 + 50) / 100 > 160) { this.Resources.Add("CColor", new SolidColorBrush(Color.FromRgb(0, 0, 0))); }
            else { this.Resources.Add("CColor", new SolidColorBrush(Color.FromRgb(255, 255, 255))); }
        }
        private void WindowMove(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            var story = (Storyboard)this.Resources["WindowDisappearAnimation"];
            if (story != null)
            {
                story.Completed += delegate { Close(); };
                story.Begin(this);
            }
        }
        private void SetFileButton_Click(object sender, RoutedEventArgs e)
        {
            var fbd = new System.Windows.Forms.OpenFileDialog() { Title = "选择补丁文件", Filter = "补丁文件(*.zip)|*.zip" };
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileDirectoryLabel.Text = fbd.FileName;
                addedDirectory = true;
            }
        }
        private async void YesButton_Click(object sender, RoutedEventArgs e)
        {
            string filedirectory = FileDirectoryLabel.Text;
            if (isUpdated == true)
            {
                CloseButton_Click(null, null);
            }
            else if (addedDirectory == true)
            {
                YesButton.Content = "  正在更新  ";
                await Task.Run(() =>
                {
                    File.Copy(filedirectory, GlobalValues.GameDir + "\\p.zip", true);
                    IFunc.FileExtract(GlobalValues.GameDir + "\\p.zip");
                });
                if (!String.IsNullOrEmpty(VersionLabel.Text))
                {
                    GlobalValues.LocalVersion = VersionLabel.Text;
                }
                IFunc.WriteConfig("LocalVersion", GlobalValues.LocalVersion);
                YesButton.Content = "  更新完成  ";
                if (File.Exists(GlobalValues.GameDir + "\\p.zip")) File.Delete(GlobalValues.GameDir + "\\p.zip");
                YesButton.BorderBrush = new SolidColorBrush(Color.FromRgb(77, 177, 77));
                isUpdated = true;
            }
            else
            {
                YesButton.Content = "  更新无效  ";
                YesButton.BorderBrush = new SolidColorBrush(Color.FromRgb(233, 22, 33));
                isUpdated = true;
            }
        }


        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.Space)
            {
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4)
            {
                e.Handled = true;
                CloseButton_Click(null, null);
            }
            else if (e.Key == Key.Escape) CloseButton_Click(null, null);
        }

        private void VersionLabel_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) YesButton_Click(null, null);
        }
    }
}
